<?php
class Admin_Form_Changepass{
	
	// Thuoc tinh chua thong bao loi cua form
	protected $_messagesError = null;
	
	// Mang chua du lieu sau khi kiem tra
	protected $_arrData;
	
	public function __construct($arrParam = array(), $options = null){
                // kiem tra old password
                if (!$this->checkOldPass($arrParam)){
                    $this->_messagesError['old_password'] = 'Old password not match';
                }
		// Kiem tra new password
		$validator = new Zend_Validate();
                $validator->addValidator(new Zend_Validate_NotEmpty(), true)
                                  ->addValidator(new Zend_Validate_StringLength(8,32), true)
                                  ->addValidator(new Zend_Validate_Regex('#^[a-zA-Z0-9@\#\$%\^$\*\-\+]+$#'), true);
                if(!$validator->isValid($arrParam['new_password'])){
                        $message = $validator->getMessages();
                        $this->_messagesError['new_password'] = 'New password: ' . current($message);
                }
                
                // check confirm pass
                if ($arrParam['new_password'] != $arrParam['confirm_password']){
                    $this->_messagesError['confirm_password'] = 'Confirm password not match';
                }
		
		// Truyen cac gia tri dung vao mang arrData
		$this->_arrData = $arrParam;
		
	}
	
	// Kiem tra, neu co loi tra ve gia tri True
	public function isError(){
		if(count($this->_messagesError)>0){
			return true;
		}else{
			return false;
		}
	}
	
	// Tra ve mot mang cac thong bao loi
	public function getMessageError(){
		return $this->_messagesError;
	}
	
	// Tra ve mot mang du lieu sau khi kiem tra
	public function getData($options = null){
		return $this->_arrData;
	}
        
        public function checkOldPass($arrParam){
            $encode             = new Australian_Encode();
            $currUsername       = $arrParam['userinfor']['username'];
            $oldPass            = $arrParam['old_password'];
            $db                 = Zend_Registry::get('connectDb');
            $select             = $db->select()->from('user', array('password'))->where("username = '$currUsername' ");
            $rs                 = $db->fetchAll($select);
            $password           = $rs[0]['password'];
            return ( $encode->password($oldPass) == $password);
        }
	
	
}




